package cn.swu.cs.lessonseven

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TableLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class MainActivity : AppCompatActivity() {

    val category = arrayListOf<String>()

    lateinit var adapter: NewsAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()
        initData()
    }

    private fun initData() {

        category.add("头条")
        category.add("房产")
        category.add("教育")
        category.add("军事")
        category.add("游戏")
        category.add("八卦")
        category.add("八卦2")
        category.add("八卦3")
        category.add("八卦4")
        category.add("八卦5")

        adapter.setData(category)

    }

    private fun initView() {
        val tabLayout = findViewById<TabLayout>(R.id.tab_layout)
        val viewpager = findViewById<ViewPager2>(R.id.viewpager)

        adapter = NewsAdapter(this)
        viewpager.adapter = adapter

        TabLayoutMediator(tabLayout,viewpager){ tab,pos->
            val item = adapter.getItem(pos)

            tab.text = item
        }.attach()


    }

}